package 牛客_面试必刷.动态规划;

public class 最长公共子序列_2 {
    public String LCS (String s1, String s2) {
        // write code here
        int m = s1.length(), n = s2.length();
        String[][] dp = new String[m + 1][n + 1];
        String res = "";
        for (int i = 0; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                if (i == 0 || j == 0) {
                    dp[i][j] = "";
                } else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + s1.charAt(i - 1);
                } else {
                    dp[i][j] = dp[i - 1][j].length() > dp[i][j - 1].length() ? dp[i - 1][j] : dp[i][j - 1];
                }
            }
        }
        if (dp[m][n] == "") return "-1";
        return dp[m][n];
    }
}
